package com.zjzywl.iotSendthings.serviceImpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.zjzywl.iotSendthings.entity.JDWPBGMX;
import com.zjzywl.iotSendthings.entity.JDWPBGXX;
import com.zjzywl.iotSendthings.entity.WPJSZPB;
import com.zjzywl.iotSendthings.repository.InsertJDWPBGXXRepository;
import com.zjzywl.iotSendthings.repository.InsertWPJSZPBRepository;
import com.zjzywl.iotSendthings.repository.ProvideJDWPBGMXRepository;
import com.zjzywl.iotSendthings.repository1.OrclJDWPBGMXRepository;
import com.zjzywl.iotSendthings.repository1.OrclJDWPBGXXRepository;
import com.zjzywl.iotSendthings.repository1.OrclWPJSZPBRepository;
import com.zjzywl.iotSendthings.service.UpdataInformationService;
import com.zjzywl.iotSendthings.util.Item;
import com.zjzywl.iotSendthings.util.WebResult;
@Service
public class UpdataInformationServiceImpl implements UpdataInformationService{
    @Autowired
    private ProvideJDWPBGMXRepository JDWPBGMXRepository;
    @Autowired
    private InsertJDWPBGXXRepository insertRepository;
    @Autowired
    private InsertWPJSZPBRepository WPJSZPBRepository;
    @Autowired
    private OrclJDWPBGXXRepository orclJDWPBGXXRepository;
    @Autowired
    private OrclJDWPBGMXRepository orclJDWPBGMXRepository;
    @Autowired
    private OrclWPJSZPBRepository orclWPJSZPBRepository;
    // 看守所编号
    @Value("${app.kssbh}")
    private String kssbh;
    
    @Override
    public void getInformation(Item item,List<String> sls, List<String> wps, String uuid, String jsr) {
        com.zjzywl.iotSendthings.entity1.JDWPBGXX save1=new com.zjzywl.iotSendthings.entity1.JDWPBGXX();
        try {
            for (int i = 0; i < sls.size(); i++) {
                String wp = wps.get(i);
                String sl = sls.get(i);
                String bz = item.getBz();
                JDWPBGMXRepository.updataByUuid(wp, sl, uuid, i + "", bz);
                insertRepository.updateJDWPBGXX(uuid, bz, jsr);
            }
            // orcal存 JDWPBGXX表
            System.out.println(uuid);
            JDWPBGXX findByuuid = insertRepository.findByuuid(uuid);
            System.out.println("ssssssss" + findByuuid.getZybh());
            com.zjzywl.iotSendthings.entity1.JDWPBGXX jdwpbgxx = new com.zjzywl.iotSendthings.entity1.JDWPBGXX();
            System.out.println("zybh" + findByuuid.getZybh());
            jdwpbgxx.setZybh(findByuuid.getZybh());
            jdwpbgxx.setRybh(findByuuid.getRybh());
            jdwpbgxx.setDjbh(findByuuid.getDjbh());
            jdwpbgxx.setBgrq(findByuuid.getBgrq());
            jdwpbgxx.setJsr(findByuuid.getJsr());
            jdwpbgxx.setBgr(findByuuid.getBgr());
            jdwpbgxx.setSjc(findByuuid.getSjc());
            jdwpbgxx.setSwrxm(findByuuid.getSwrxm());
            jdwpbgxx.setSwrzjh(findByuuid.getSwrzjh());
            jdwpbgxx.setBz(findByuuid.getBz());
            jdwpbgxx.setBglx(findByuuid.getBglx());
            jdwpbgxx.setXh(findByuuid.getXh());
            jdwpbgxx.setLqbj(findByuuid.getLqbj());
            jdwpbgxx.setLqr(findByuuid.getLqr());
            jdwpbgxx.setLqrq(findByuuid.getLqrq());
            jdwpbgxx.setYzyrygx(findByuuid.getYzyrygx());
            jdwpbgxx.setLxdh(findByuuid.getLxdh());
            jdwpbgxx.setSwrdz(findByuuid.getSwrdz());
            jdwpbgxx.setJsfs(findByuuid.getJsfs());
            save1 = orclJDWPBGXXRepository.save(jdwpbgxx);

            // orcal存 JDWPBGMX表
            Date Time = new Date();
            SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMdd");
            String Str = format1.format(Time);
            String sub = Str.substring(1, Str.length());
            com.zjzywl.iotSendthings.entity1.JDWPBGMX mx = new com.zjzywl.iotSendthings.entity1.JDWPBGMX();
            List<JDWPBGMX> list = JDWPBGMXRepository.findByUuid(uuid);
            for (JDWPBGMX jdwpbgmx : list) {
                if (!"0".equals(jdwpbgmx.getSl())) {
                    String Mxzybh = orclJDWPBGMXRepository.findByZybh();
                    if (Mxzybh == null || Mxzybh.indexOf(sub) == -1) {
                        mx.setZybh(kssbh + sub + 1000);
                    } else {
                        String substring = Mxzybh.substring(Mxzybh.length() - 4, Mxzybh.length());
                        int parseInt = Integer.parseInt(substring);
                        ++parseInt;
                        System.out.println("jdZybh" + kssbh + sub + parseInt);
                        mx.setZybh(kssbh + sub + parseInt);
                    }
                    System.out.println("jdwpbgmx" + jdwpbgmx.getZybh());

                    mx.setBgzybh(jdwpbgmx.getBgzybh());
                    mx.setWp(jdwpbgmx.getWp());
                    mx.setSl(jdwpbgmx.getSl());
                    mx.setGg(jdwpbgmx.getGg());
                    mx.setTz(jdwpbgmx.getTz());
                    mx.setBz(jdwpbgmx.getBz());
                    mx.setSjc(jdwpbgmx.getSjc());
                    orclJDWPBGMXRepository.save(mx);
                    System.out.println("物品明细表保存成功");
                }
            }

            com.zjzywl.iotSendthings.entity1.WPJSZPB wpj = new com.zjzywl.iotSendthings.entity1.WPJSZPB();
            List<WPJSZPB> wp = WPJSZPBRepository.findByUuid(uuid);
            int i = 1;
            for (WPJSZPB wpjszpb : wp) {
                wpj.setZpzybh(wpjszpb.getZpzybh());
                wpj.setWpjszybh(wpjszpb.getWpjszybh());
                wpj.setSjc(wpjszpb.getSjc());
                String xh = i + "";
                String zpzybh = wpjszpb.getZpzybh();
                WPJSZPBRepository.updataBz(zpzybh, uuid, xh);
                wpj.setXh(i++ + "");
                wpj.setZp(wpjszpb.getZp());
                orclWPJSZPBRepository.save(wpj);
                System.out.println("物品照片表存储成功");
            }
        } catch (Exception e) {
            insertRepository.updateByUuid(uuid);
            orclJDWPBGXXRepository.delete(save1);
            System.out.println(e);
            
        }
        
    }

}
